﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents.Design;
using System.Reflection;

namespace ProgNetComponents.ExtenderProviders.Settings
{
    [
    ToolboxItem(false),
    DesignTimeVisible(false),
    TypeConverter(typeof(ExpandableObjectConverter))
    ]
    public class BaseExtenderSettings : ProgNetComponents.Components.CustomTypeDescriptorObject, IComponent, ISupportInitialize
    {
        public override PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection properties =  base.GetProperties();
            PropertyInfo[] pis = this.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            List<string> props = new List<string>();
            foreach (PropertyInfo p in pis)
                props.Add(p.Name);
            List<PropertyDescriptor> del = new List<PropertyDescriptor>();
            for (int i=0; i < properties.Count; i++)
            {
                if (!props.Contains(properties[i].Name))
                    del.Add(properties[i]);
            }
            foreach (PropertyDescriptor pd in del)
                properties.Remove(pd);

            return properties;
        }

        public override string ToString()
        {
            return this.GetType().Name;
        }
        #region Ctor
        protected BaseExtenderSettings(object Owner, BaseExtenderProvider provider)
        {
            this.Owner = Owner;
            _Provider = provider;
            this.Site = new FakeSite(this, _Provider);
        }
        #endregion

        #region Members
        protected ISite _Site;
        protected object Owner;
        protected BaseExtenderProvider _Provider;
        protected bool _Initializing;
        #endregion

        #region ISupportInitialize Members

        public void BeginInit()
        {
            _Initializing = true;
        }

        public void EndInit()
        {
            _Initializing = false;
            OnSettingsChanged();
        }

        #endregion
        
        /// <summary>
        /// Event that is fiered when settings changed
        /// </summary>
        public event EventHandler SettingsChanged;

        protected virtual void OnSettingsChanged()
        {
            if (!_Initializing)
            {
                if (SettingsChanged != null)
                {
                    SettingsChanged(Owner, EventArgs.Empty);
                }
            }
        }

        #region IComponent Members
        [Browsable(false)]
        public event EventHandler  Disposed;
        
        [Browsable(false)]
        public ISite Site
        {
            get
            {
                return _Site;
            }
            set
            {
                _Site = value;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Disposed != null)
                Disposed(this, EventArgs.Empty);
        }

        #endregion
    }
   
}
